package com.ymatou.iapi.sellercs.testcase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellercs.parameter.LeaveLineBean;
import com.ymatou.iapi.sellercs.service.LeaveLineCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.SellerCSIMCallService;
import com.ymttest.database.model.YmtOnlinecs;
import com.ymttest.database.sqlwapper.YmtSellerCSIMWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.GetLog;
import com.ymttest.utils.TestDataManager;
/**
*LeaveLine
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_LeaveLine{
	private static LeaveLineBean leavelineBean;
	private static LeaveLineCall leavelineCall;
	YmtSellerCSIMWapper YmtSellerCSIMWapper;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static int zsellerId = Integer.parseInt(EnvSetup.getData("autotest_seller_z"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("LeaveLine");
	}

	@Before
	public void caseUp() throws ClientProtocolException, IOException {
		leavelineBean=new LeaveLineBean();
		leavelineCall=new LeaveLineCall();
		YmtSellerCSIMWapper=new YmtSellerCSIMWapper();
		SellerCSIMCallService.OffLine(sellerId, null);
		SellerCSIMCallService.OffLine(zsellerId, null);
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(leavelineCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_LeaveLine_001() {
		Logger.start(true,"登入后LeaveLine成功,Status=1");
		try {
			Logger.comment("初始化数据");
			Integer csid=3383;
			Integer mcsid=3383;
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(csid);
			String SocketIoToken=TestDataManager.getRandomUUID().toString();
			SellerCSIMCallService.PcLogin(mcsid, csid, SocketIoToken);
			
			GetLog getLog=new GetLog("172.16.101.133", "sellercs.iapi.ymatou.com");
			//post 表单不接受requestid
			getLog.startListenVm("default.log",".");
			
			leavelineBean.setCSId(csid);
			leavelineCall.setData(leavelineBean);
			leavelineCall.callService();			
			Logger.verifyIsContains("true", leavelineCall.getString("Result"), "验证IsSuccess");
			Logger.verifyIsContains("200", leavelineCall.getString("Status"), "验证Status");
			Thread.sleep(3000);
			getLog.endListen();
			ArrayList<String> loglist = getLog.getLastLog();
			//验证日志 10001 op=0
			Logger.comment("验证日志包含发送指令opType=10001 op=0隐身");
			List<String> filter2 = loglist.stream().filter(s->s.contains("opType=10001")).filter(s->s.contains("op=0")).collect(Collectors.toList());
			Logger.debug(filter2);
			Logger.verifyEquals(true,filter2.size()>=1,"验证日志");
			YmtOnlinecs info = YmtSellerCSIMWapper.selectYmtOnlinecs(null, csid).get(0);
			Logger.debug(FormatUtil.beanToGSONString(info));
			Logger.verifyEquals(1, info.getStatus(), "验证Status");
			SellerCSIMCallService.OffLine(csid, SocketIoToken);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_LeaveLine_002() {
		Logger.start(true,"不输入CSId失败");
		try {
			leavelineCall.setData(leavelineBean);
			leavelineCall.callService();			
			Logger.verifyIsContains("400", leavelineCall.getString("Status"), "验证Status");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_LeaveLine_003() {
		Logger.start(true,"重复发送离线请求");
		try {
			Logger.comment("初始化数据");
			Integer csid=3383;
			Integer mcsid=3383;
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(csid);
			String SocketIoToken=TestDataManager.getRandomUUID().toString();
			SellerCSIMCallService.PcLogin(mcsid, csid, SocketIoToken);
			
			GetLog getLog=new GetLog("172.16.101.133", "sellercs.iapi.ymatou.com");
			//post 表单不接受requestid
			getLog.startListenVm("default.log",".");
			
			leavelineBean.setCSId(csid);
			leavelineCall.setData(leavelineBean);
			leavelineCall.callService();			
			leavelineCall.callService();			
			Logger.verifyIsContains("true", leavelineCall.getString("Result"), "验证IsSuccess");
			Logger.verifyIsContains("200", leavelineCall.getString("Status"), "验证Status");
			Thread.sleep(3000);
			getLog.endListen();
			ArrayList<String> loglist = getLog.getLastLog();
			//验证日志 10001 op=0
			Logger.comment("验证日志包含发送指令opType=10001 op=0隐身");
			List<String> filter2 = loglist.stream().filter(s->s.contains("opType=10001")).filter(s->s.contains("op=0")).collect(Collectors.toList());
			Logger.debug(filter2);
			Logger.verifyEquals(true,filter2.size()>=1,"验证日志");
			YmtOnlinecs info = YmtSellerCSIMWapper.selectYmtOnlinecs(null, csid).get(0);
			Logger.debug(FormatUtil.beanToGSONString(info));
			Logger.verifyEquals(1, info.getStatus(), "验证Status");
			SellerCSIMCallService.OffLine(csid, SocketIoToken);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_LeaveLine_004() {
		Logger.start(true,"输入不存在CSId失败");
		try {
			leavelineBean.setCSId(-1);
			leavelineCall.setData(leavelineBean);
			leavelineCall.callService();			
			Logger.verifyIsContains("400", leavelineCall.getString("Status"), "验证Status");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}